﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace S4Store.Models.AfterSaleReport
{
    public class SparePartModel : BaseReportModel
    {
        public List<Domain.AfterSaleKpi.SparePartsMonthInput> MonthConfigs { get; set; }
        public List<Domain.AfterSaleKpi.DeadStock> DeadStockMonthConfigs { get; set; }
        public List<Domain.AfterSaleKpi.MonthStandardConfig> StandardConfigs { get; set; }

        public List<Domain.AfterSaleKpi.SparePartsMonthInput> MonthConfig { get; set; }
        public List<Domain.AfterSaleKpi.DeadStock> DeadStockMonthConfig { get; set; }
        public List<Domain.AfterSaleKpi.MonthStandardConfig> StandardConfig { get; set; }


        public Domain.AfterSaleKpi.SparePartsMonthInput CurMonthMonthConfig(int year, int month)
        {
            var config = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (config == null) config = new Domain.AfterSaleKpi.SparePartsMonthInput();
            return config;
        }

        public Domain.AfterSaleKpi.MonthStandardConfig CurMonthStandardConfig(int year, int month)
        {
            var config = StandardConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (config == null) config = new Domain.AfterSaleKpi.MonthStandardConfig();
            return config;
        }

        /// <summary>
        /// 呆滞库变化率
        /// </summary>
        /// <returns></returns>
        public decimal DZKBHL(int year,int month)
        {
            //=SUM(W6-W5)
            //呆   库    存   率(本月) - 呆   库    存   率（上月）
            var date = new DateTime(year, month, 1);
            var lastMonth = date.AddMonths(-1);
            return DKCL(year, month) - DKCL(lastMonth.Year, lastMonth.Month);
        }


        /// <summary>
        /// 呆库存率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal DKCL(int year, int month)
        {
            //=SUM(L6/B6)
            //=呆         滞       库      存     备      件       成             本/库    存    成   本   总   量  目  标
            if (MonthConfigs == null) return 0;
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.DZKCBJCB == 0 || M.KCCBZL == 0) return 0;
            return Math.Round(M.DZKCBJCB / M.KCCBZL, 2, MidpointRounding.AwayFromZero);
        }


        /// <summary>
        /// 死库存率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal SKCL(int year, int month)
        {
            //=SUM(L6/B6)
            if (MonthConfigs == null) return 0;
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.SKCBJCB == 0 || M.KCCBZL == 0) return 0;
            return Math.Round(M.SKCBJCB / M.KCCBZL, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 死库变化率
        /// </summary>
        /// <returns></returns>
        public decimal SKBHL(int year, int month)
        {
            //=SUM(W6-W5)
            //死库存率(本月) - 死库存率（上月）
            var date = new DateTime(year, month, 1);
            var lastMonth = date.AddMonths(-1);
            return SKCL(year, month) - SKCL(lastMonth.Year, lastMonth.Month);
        }

        /// <summary>
        /// 备件满足率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal BJMZL(int year, int month)
        {
            //=SUM(1-(P6/Q6))
            //1-缺   件  未       维        修      台          次/月          维           修          总          台          次
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.QJWWXTC == 0 || M.YWXZTC == 0) return 0;
            return Math.Round(((decimal)1 - (decimal)M.QJWWXTC / (decimal)M.YWXZTC)*100, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 备件周转率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal BJZZL(int year, int month)
        {
            //=SUM((G6*12)/E6)
            //月     备       件      销        售        成          本*12/本          月           库          存              均               值
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null) return 0;

            var 本月库存均值 = BYKCJZ(year, month);
            if (本月库存均值 == 0) return 0;

            return Math.Round(M.YBJXSCB * 12 / 本月库存均值, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// //本月库存均值
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal BYKCJZ(int year, int month)
        {
            //=AVERAGE(C6:D6)
            //(月   初     库   存   成   本+月   末   库   存   成   本)/2
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null) return 0;
            return (M.YCKCCB + M.YMKCCB) / 2;
        }

        /// <summary>
        /// 备   件   保   修   合   格   率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal BJBXHGL(int year, int month)
        {
            //=SUM(O6/N6)
            //保       修        合         格       件              数/申       请          保      修          总         件           数
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.BXHGJS == 0 || M.SQBXZJS == 0) return 0;
            return Math.Round(((decimal)M.BXHGJS/(decimal)M.SQBXZJS) * 100, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 正常订货率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal ZCDHL(int year, int month)
        {
            //=SUM(H6/I6)
            //月           计            划              订            货           成            本/月          全          部           订       货        成         本
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.YJHDHCB == 0 || M.YQBDHCB == 0) return 0;
            return Math.Round(((decimal)M.YJHDHCB / (decimal)M.YQBDHCB) * 100, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 平均在库天数
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public decimal PJZKTS(int year, int month)
        {
            //库    存    成   本   总   量  目  标/日    均   备   件   销   售   成   本
            var M = MonthConfigs.Where(o => o.Month.Year == year && o.Month.Month == month).FirstOrDefault();
            if (M == null || M.KCCBZL == 0 || M.RJBJXSCB == 0) return 0;
            return Math.Round(((decimal)M.KCCBZL / (decimal)M.RJBJXSCB), 0, MidpointRounding.AwayFromZero);
        }
    }
}